<?php

defined('IN_MY_PHP') || die(0);

// 初始化本文件单独使用时涉及到的常量
defined('PAGE_ROW_COUNT') || define('PAGE_ROW_COUNT', 20);
/**
 * 一个简单的分页类.
 *
 * @author netmou <leiyanfo@sina.com>
 */
class Page
{
	private $rowCount; //每页显示的记录条数
	private $offset; //当前页记录开始位置
	private $total; //记录总条数
	private $curPage; //当前被选中的页
	private $pageTotal; //分页总数
	private $pageLink; //每个分页的链接

	/**
	 * @param offset   每页显示的条目数
	 * @param total	 总条目数
	 * @param curPage   当前被选中的页
	 * @param pageLink	每个分页的链接
	 */
	public function __construct($total, $queryData = null, $rowCount = null, $page = 'page')
	{
		$this->rowCount = ($rowCount == null) ? PAGE_ROW_COUNT : intval($rowCount);
		$this->total = intval($total);
		$this->pageTotal = max(1, ceil($total / $this->rowCount));
		$this->curPage = max(1, intval($_GET[$page]));
		$this->offset = ($this->curPage - 1) * $this->rowCount;
		$queryStr = '';
		if ($queryData != null && is_array($queryData)) {
			unset($queryData[$page]);
			$queryStr = http_build_query($queryData, '', '&');
		} elseif ($queryData != null && is_string($queryData)) {
			$queryStr = preg_replace('/(^|&|\\?)' . preg_quote($page,'/') . '=[^&#]*/s', '', $queryData);
			$queryStr = ltrim($queryStr, '&?');
		}
		if ($queryStr != '') {
			$this->pageLink = "?{$queryStr}&{$page}=";
		} else {
			$this->pageLink = "?{$page}=";
		}
		$this->pageLink = htmlspecialchars($this->pageLink);
	}

	/**
	 * @return int 分页开始的位置
	 */
	public function getRowCount()
	{
		return $this->rowCount;
	}
	/**
	 * @return int 分页偏移量
	 */
	public function getOffset()
	{
		return $this->offset;
	}
	/**
	 * @param $list   每次显示的页数
	 * @param $jumpMax   跳转框每次向前，向后显示页码数 (乘2)
	 */
	public function showLinks($list = 6, $jumpMax = 50)
	{
		$pageFragment = null;
		$from = max(floor($this->curPage - $list / 2), 1);
		$to = min(ceil($this->curPage + $list / 2), $this->pageTotal);
		$pageFragment .= "<span class=\"page page-total\">共 {$this->total} 条</span>";
		$pageFragment .= "<span class=\"page page-count\">每页 {$this->rowCount} 条</span>";
		$pageFragment .= "<span class=\"page page-process\">第 {$this->curPage}/{$this->pageTotal} 页</span>";
		if ($from > 1) {
			$homePage = $this->pageLink.'1';
			$pageFragment .= "<a href=\"{$homePage}\" class=\"page page-first lnk\">首页</a>";
		}
		if ($this->curPage > 1) {
			$prevPage = $this->pageLink . ($this->curPage - 1);
			$pageFragment .= "<a  href=\"{$prevPage}\" class=\"page page-prev lnk\">上一页</a>";
		}
		if ($this->pageTotal > 1) {
			for ($i = $from; $i < $this->curPage; ++$i) {
				$pageAddr = $this->pageLink . $i;
				$pageFragment .= "<a href=\"{$pageAddr}\" class=\"page page-digit lnk\">{$i}</a>";
			}
			$pageFragment .= "<span class=\"page page-digit-cur\">{$this->curPage}</span>";
			for ($i = $this->curPage + 1; $i <= $to; ++$i) {
				$pageAddr = $this->pageLink . $i;
				$pageFragment .= "<a href=\"{$pageAddr}\" class=\"page page-digit lnk\">{$i}</a>";
			}
		}

		if ($this->curPage < $this->pageTotal) {
			$nextPage = $this->pageLink . ($this->curPage + 1);
			$pageFragment .= "<a  href=\"{$nextPage}\" class=\"page page-next lnk\">下一页</a>";
		}
		if ($to < $this->pageTotal) {
			$rearPage = $this->pageLink.$this->pageTotal;
			$pageFragment .= "<a href=\"{$rearPage}\" class=\"page page-last lnk\">尾页</a>";
		}
		if ($this->pageTotal > $list) {
			$pageFragment .= '<label class="page page-jump-label">跳转到 <select class="page-jump" style="line-height:1;padding:0 2px" onchange="window.location=\'' . $this->pageLink . '\'+this.value">';
			$jumpStart = max($from - $jumpMax, 1);
			for ($i = $jumpStart;$i <= $from - 1;++$i) {
				if ($this->curPage == $i) {
					$pageFragment .= "<option selected value=\"{$i}\">$i</option>";
					continue;
				}
				$pageFragment .= "<option value=\"{$i}\">$i</option>";
			}
			$pageFragment .= '<option selected disabled>...</option>';
			$jumpEnd = min($jumpMax + $to, $this->pageTotal);
			for ($i = $to + 1;$i <= $jumpEnd;++$i) {
				if ($this->curPage == $i) {
					$pageFragment .= "<option selected value=\"{$i}\">$i</option>";
					continue;
				}
				$pageFragment .= "<option value=\"{$i}\">{$i}</option>";
			}

			$pageFragment .= '</select></label>';
		}

		return $pageFragment;
	}
}
// use example:
// $input = $func->inputData();
// $sql = "select * from `xxx` where 1=1 {$condition}";
// $total = $pdo->resultCount($sql);
// $page = new Page($total, $input);
// $sql .= " order by xxx desc limit {$page->getOffset()}, {$page->getRowCount()}";
// $stmt = $pdo->query($sql);
// while($info = $stmt->fetch()):
//   // do ...
// endwhile;
// $page->showLinks();